package cn.lyq.card.ui.share

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.ShareImageInfo
import cn.lyq.card.manager.UserManager
import cn.lyq.card.utils.QrUtils
import cn.lyq.card.utils.ShareUtils
import cn.lyq.card.utils.loadUrl
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_share_img.*
import kotlin.concurrent.thread


class ShareImgActivity : BaseActivity() {

    private val mInfo by lazy {
        intent.getSerializableExtra(KEY_INFO)
            ?.takeIf { it is ShareImageInfo }
            ?.run { this as ShareImageInfo }
    }

    companion object {
        private const val KEY_INFO = "INFO"

        @JvmStatic
        fun start(context: Context, info: ShareImageInfo?) {
            val starter = Intent(context, ShareImgActivity::class.java)
                .putExtra(KEY_INFO, info)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.act_share_img
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_share_image)

        mInfo?.let {
            img.loadUrl(it.image)
            val width = resources.getDimensionPixelOffset(R.dimen.width_qr)
            val bitmapQr = QrUtils.createQRCodeBitmap(it.url ?: "", width, width)
            imgQr.setImageBitmap(bitmapQr)
            val user = UserManager.instance.value
            tvCommandName.text = "推荐人昵称：${user?.userName ?: user?.trueName ?: ""}"
            tvCommandPhone.text = "推荐人手机：${UserManager.getPhone()}"
        }

    }

    override fun initListener() {
        super.initListener()
        tvWeChat.setClick {
            thread {
                ImageUtils.save2Album(ImageUtils.view2Bitmap(groupImg), Bitmap.CompressFormat.JPEG)
                    ?.run {
                        ShareUtils.getImgUri(this@ShareImgActivity, this)
                    }?.run {
                        ShareUtils.build().shareWeChatFriend().shareImage(this).create()
                    }?.let { startActivity(it) }
            }
        }
        tvFriend.setClick {
            thread {
                ImageUtils.save2Album(ImageUtils.view2Bitmap(groupImg), Bitmap.CompressFormat.JPEG)
                    ?.run {
                        ShareUtils.getImgUri(this@ShareImgActivity, this)
                    }?.run {
                        ShareUtils.build().shareWeChatFriendCircle().shareImage(this).create()
                    }?.let { startActivity(it) }
            }
        }
    }

    override fun initObserver() {
        super.initObserver()
        PermissionUtils.permission(PermissionConstants.STORAGE)
            .callback(object : PermissionUtils.FullCallback {
                override fun onGranted(permissionsGranted: List<String>) {
                    LogUtils.d(permissionsGranted)
                }

                override fun onDenied(
                    permissionsDeniedForever: List<String>,
                    permissionsDenied: List<String>
                ) {
                    ToastUtils.showShort(R.string.str_permission_denied_to_share_img_tip)
                    LogUtils.d(permissionsDeniedForever, permissionsDenied)
                }
            })
            .request()
    }
}